local util = require 'lspconfig.util'
local lsp = vim.lsp

local M = {}

local function fix_all(opts)
	opts = opts or {}

	local stylelint_client = util.get_active_client_by_name(opts.bufnr, 'stylelint_lsp')
	if stylelint_client == nil then
		return
	end

	local request
	if opts.sync then
		request = function(bufnr, method, params)
			stylelint_client.request_sync(method, params, nil, bufnr)
		end
	else
		request = function(bufnr, method, params)
			stylelint_client.request(method, params, nil, bufnr)
		end
	end

	local bufnr = util.validate_bufnr(opts.bufnr or 0)
	request(0, 'workspace/executeCommand', {
		command = 'stylelint.applyAutoFixes',
		arguments = {
			{
				uri = vim.uri_from_bufnr(bufnr),
				version = lsp.util.buf_versions[bufnr],
			},
		},
	})
end

M.filetypes = {
	'css',
	'less',
	'scss',
	'sugarss',
	'vue',
	'wxss',
	'javascriptreact',
	'typescriptreact'
}
M.commands = {
	StyleFixAll = {
		function()
			fix_all { sync = true, bufnr = 0 }
		end,
		description = 'Fix all stylelint problems for this buffer',
	},
}


return M
